package no.ffi.gismoidm.pubsub;

import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.net.ServerSocket;
import java.net.Socket;

public class TestOOConnection implements Runnable {

    public static void main(String[] args) throws Exception {
        new TestOOConnection();
    }
    
    Socket s;
    ObjectInputStream ois;
    
    public TestOOConnection() throws Exception {
        ServerSocket ss = new ServerSocket(4444);
        //new Thread(this).start();
        new Other();
        s = ss.accept();
        ObjectOutputStream oos = new ObjectOutputStream(s.getOutputStream());
        ois = new ObjectInputStream(s.getInputStream());
        while (true) {
            String str = (String)ois.readObject();
            System.out.println(str);
            s.close();
        }
    }
    
    public void run() {
        try { 
            Thread.sleep(3000);
            ois.close();
        } catch (Exception e) { e.printStackTrace(); }
        
    }
    
    class Other implements Runnable {
        public Other() { new Thread(this).start(); }
        public void run() {
            try {
                Thread.sleep(1000);
                Socket s = new Socket("127.0.0.1",4444);
                ObjectOutputStream oos = new ObjectOutputStream(s.getOutputStream());
                System.out.println("Connected");
                Thread.sleep(10000);
                System.out.println("Disconnecting");
                s.close();
            } catch (Exception e) { e.printStackTrace(); }
        }
    }
}
